package com.example.superoa.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.common.Shijian;
import com.example.superoa.model.dao.StaffDao;
import com.example.superoa.model.pojos.KqOverwork;
import com.example.superoa.model.pojos.KqOverworkXiang;
import com.example.superoa.model.pojos.Staff;
import com.example.superoa.model.service.AttendMonthService;
import com.example.superoa.model.service.DemoService;
import com.example.superoa.model.service.OvertimeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Controller
public class OvertimeController {
	@Autowired
	private OvertimeService ser;
	
	
	@Autowired
	private DemoService d_ser;
	
	
	Shijian sj=new Shijian();
	//kq-overtime-assessor 页面table初始化值
//	@RequestMapping("overtime_selectAll")
//	@ResponseBody
//	public Map<String,Object> overtimeSelectAll(int page,int rows) {
//		Map<String,Object> map = new HashMap<>();
//		//先分页
//		Page<Object> pg = PageHelper.startPage(page, rows);
//		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//		SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd HH:mm");
//		JSONArray json = ser.selectAll();
//		int i=1;
//		for (Object object : json) {
//			JSONObject obj=(JSONObject) object;
//			int shen=obj.getInteger("O_SHEN");
//			JSONObject list=(JSONObject) ser.selectXiang(obj.getInteger("O_ID")).get(0);
//			String str="";
//			if(shen==0) {
//				str="未审核";
//			}else if(shen==1) {
//				str="通过";
//			}else {
//				str="未通过";
//			}
//			if(list.getInteger("OX_TYPE")==0) {
//				obj.put("type", "工作日加班");
//				obj.put("O_KAI", sdf1.format(obj.getDate("O_KAI")));
//				obj.put("O_JIE", sdf1.format(obj.getDate("O_JIE")));
//				obj.put("O_TIME", obj.get("O_TIME")+"  /小时");
//			}else if(list.getInteger("OX_TYPE")==1){
//				obj.put("type", "休息日加班");
//				obj.put("O_KAI", sdf.format(obj.getDate("O_KAI")));
//				obj.put("O_JIE", sdf.format(obj.getDate("O_JIE")));
//				obj.put("O_TIME", obj.get("O_TIME")+"  /天");
//			}else {
//				obj.put("type", "节假日加班");
//				obj.put("O_KAI", sdf.format(obj.getDate("O_KAI")));
//				obj.put("O_JIE", sdf.format(obj.getDate("O_JIE")));
//				obj.put("O_TIME", obj.get("O_TIME")+"  /天");
//			}	
//			obj.put("o_sta", str);
//			obj.put("sortable", i);
//			i++;
//		}
//		//总记录
//		map.put("total", pg.getTotal());
//		//页内容
//		map.put("rows", json);
//		return map;
//	}
	//kq-overtime-statistical 页面table初始化值
	@RequestMapping("ostatis_selectAll")
	@ResponseBody
	public Map<String,Object> statisSelectAll(Date date,int page,int rows) {
		Map<String,Object> map = new HashMap<>();
		//先分页
		if(date==null) {
			date=new Date(sj.getNetworkTime());
		}
		List<Integer> mon=sj.getyearmonth(date);
		Page<Object> pg = PageHelper.startPage(page, rows);
		JSONArray json =new JSONArray();
		JSONArray list=d_ser.selectKuang();
		int i=1;
		for (Object o : list) {
			JSONObject obj=new JSONObject();
			JSONObject ob=(JSONObject)o;
			JSONObject objj=ser.selectStatical(mon.get(0), mon.get(1), obj.getInteger("staff_id"));
			BigDecimal one,two,three;
			if(ob.getBigDecimal("OS_PING")==null) {
				one=new BigDecimal(0);
			}else {
				one=objj.getBigDecimal("OS_PING");
			}
			if(ob.getBigDecimal("OS_XIU")==null) {
				two=new BigDecimal(0);
			}else {
				two=objj.getBigDecimal("OS_XIU");
			}
			if(ob.getBigDecimal("OS_JIE")==null){
				three=new BigDecimal(0);
			}else {
				three=objj.getBigDecimal("OS_JIE");
			}
			obj.put("sortable", i);
			obj.put("staff_id", ob.getInteger("staff_id"));
			obj.put("Staff_name", ob.getString("Staff_name"));
			obj.put("depart_name", ob.getString("depart_name"));
			obj.put("one",one);
			obj.put("two",two);
			obj.put("three",three);
			i++;
			json.add(obj);
		}
		//总记录
		map.put("total", pg.getTotal());
		//页内容
		map.put("rows", json);
		return map;
	}
	@RequestMapping("jiabantype")
	@ResponseBody
	public JSONArray selectShen() {
		String[] str= {"工作日加班","休息日加班","节假日加班"};
		JSONArray arr=new JSONArray();
		for (int i = 0; i < 3; i++) {
			JSONObject o=new JSONObject();
			o.put("id", i);
			o.put("idd", i+1);
			o.put("name", str[i]);
			arr.add(o);
		}
		return arr;
	}
	//新增加班记录
	@RequestMapping("add_overtime")
	@ResponseBody
	public String selectShen(Date date,String oWhy,Integer type,Integer tian,HttpSession session) {
		KqOverwork over=new KqOverwork();
		over.setOKai(date);
		ser.addovertime(over,oWhy,type,tian,Integer.parseInt(session.getAttribute("sid").toString()));
		return "";
	}	
	//判断加班
	@RequestMapping("overpan")
	@ResponseBody
	public String panduan(Integer type,Integer tian,Date date) {
		System.err.println(type);
		return ser.pan(type, tian,date);	
	}	
	
}
